//
//  ZYDateSelectView.m
//  YiLin
//
//  Created by Zyark on 2018/6/7.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYDateSelectView.h"
#import "ZYViewUtils.h"
#import "ZYImageUtils.h"
#import "ZYDatePickerView.h"
#import "ZYModeTypeModel.h"

@interface ZYDateSelectView()<ZYDatePickerViewDelegate>
@property(nonatomic,strong) UIView *bigBgView;
@property(nonatomic,strong) UIView *bgView;
@property(nonatomic,strong) UILabel *startTimeLabel;
@property(nonatomic,strong) UILabel *endTimeLabel;
@property(nonatomic,strong) ZYDatePickerView *startDatePicker;
@property(nonatomic,strong) ZYDatePickerView *endDatePicker;
@property(nonatomic,strong) UIButton *confirmButton;
@property(nonatomic,assign) BOOL isAlterSelectView;
@end

@implementation ZYDateSelectView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

-(void)setup{
    _bigBgView = [[UIView alloc] initWithFrame:self.bounds];
    _bigBgView.backgroundColor = RGBA(0, 0, 0, 0.3);
    [self addSubview:_bigBgView];
    
    _realHeight = self.height;
    CGFloat width = self.width * 0.92;
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, width * 0.68)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    _bgView.layer.cornerRadius = 5;
    _bgView.layer.masksToBounds = YES;
    _bgView.centerX = self.centerX;
    _bgView.centerY = self.centerY - _bgView.height / 4;
    
    //开始时间
    _startTimeLabel = [self setupLabel];
    _startTimeLabel.left = 0;
    _startTimeLabel.top = 20;
    _startTimeLabel.text = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_QUICK_SETUP_START_TIME_LABEL];
    
    //结束时间
    _endTimeLabel = [self setupLabel];
    _endTimeLabel.left = _startTimeLabel.right;
    _endTimeLabel.top = _startTimeLabel.top;
    _endTimeLabel.text = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_QUICK_SETUP_END_TIME_LABEL];
    
    //开始时间选择
    _startDatePicker = [self setupDatePicker];
    _startDatePicker.centerX = _startTimeLabel.centerX;
    _startDatePicker.top = _startTimeLabel.bottom + 10;
    _startDatePicker.hour = 11;
    _startDatePicker.minute = 5;
    
    //结束时间选择
    _endDatePicker = [self setupDatePicker];
    _endDatePicker.centerX = _endTimeLabel.centerX;
    _endDatePicker.top = _startDatePicker.top;
    
    //确认
    _confirmButton = [ZYViewUtils setupConfirmBtn];
    _confirmButton.size = CGSizeMake(width / 3, width * 0.12);
    [_bgView addSubview:_confirmButton];
    _confirmButton.centerX = _bgView.width / 2;
    _confirmButton.top = _startDatePicker.bottom + 10;
    [_confirmButton addTarget:self action:@selector(doConfirm:) forControlEvents:UIControlEventTouchUpInside];
    [_confirmButton setTitle:[[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_CONFIRM_BUTTON_TITLE] forState:UIControlStateNormal];
    
    [self setupLine];
    
    self.alpha = 0;
    self.bigBgView.hidden = YES;
    self.top = -self.height;
    self.backgroundColor = [UIColor clearColor];
}

-(UILabel *)setupLabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, _bgView.width / 2, 20)];
    label.textColor = RGBA(102, 102, 102, 1);
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16];
    [_bgView addSubview:label];
    return label;
}

-(void)setupLine{
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = RGBA(235, 235, 235, 1);
    line.size = CGSizeMake(1, _startDatePicker.height * 0.7);
    [_bgView addSubview:line];
    line.centerX = _bgView.width / 2;
    line.centerY = _startDatePicker.centerY;
}

-(ZYDatePickerView *)setupDatePicker{
    ZYDatePickerView *picker = [[ZYDatePickerView alloc] initWithFrame:CGRectMake(0, 0, _bgView.width * 0.4, _bgView.height * 0.5)];
    [_bgView addSubview:picker];
    picker.delegate = self;
    return picker;
}


#pragma mark public
-(void)showOrHidden{
    _isAlterSelectView = !_isAlterSelectView;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

-(void)close{
    _isAlterSelectView = NO;
    [UIView animateWithDuration:0.3 animations:^{
        if(self.isAlterSelectView){
            self.alpha = 1;
            self.top = 0;
        }else{
            self.bigBgView.hidden = YES;
            self.alpha = 0;
            self.top = -self.height;
        }
    } completion:^(BOOL finished) {
        self.bigBgView.hidden = !self.isAlterSelectView;
    }];
}

#pragma mark - Event

-(void)doConfirm:(UIButton *)button{
    if([_delegate respondsToSelector:@selector(dateSelectView:didSelectWithModel:)]){
        [_delegate dateSelectView:self didSelectWithModel:_model];
    }
    [self close];
}

#pragma mark - ZYDatePickerViewDelegate
-(void)datePickerView:(ZYDatePickerView *)datePickerView didSelectWithHour:(NSInteger)hour minute:(NSInteger)minute{
    NSString *minuteStr = @"";
    if(minute > 10){
        minuteStr = [NSString stringWithFormat:@"%ld",minute];
    }else{
        minuteStr = [NSString stringWithFormat:@"0%ld",minute];
    }
    NSString *hourStr = @"";
    if(hour > 10){
        hourStr = [NSString stringWithFormat:@"%ld",hour];
    }else{
        hourStr = [NSString stringWithFormat:@"0%ld",hour];
    }
    if(datePickerView == _startDatePicker){
        _model.startMinute = minuteStr;
        _model.startHour = hourStr;
    }else{
        _model.endMinute = minuteStr;
        _model.endHour = hourStr;
    }
}

-(void)setModel:(ZYModeTypeModel *)model{
    _model = model;
    NSInteger startHour = [model.startHour integerValue];
    NSInteger startMinute = [model.startMinute integerValue];
    NSInteger endHour = [model.endHour integerValue];
    NSInteger endMinute = [model.endMinute integerValue];
    _startDatePicker.hour = startHour;
    _startDatePicker.minute = startMinute;
    
    _endDatePicker.hour = endHour;
    _endDatePicker.minute = endMinute;
}

@end
